package app

import (
	"chatgpt/helper"
	"chatgpt/helper/code"
	"chatgpt/logq"
	"chatgpt/model/app"
	"chatgpt/services/datasrv"
	dbapp "chatgpt/services/datasrv/app"
	"chatgpt/services/datasrv/sqlhelper"
	"context"
	"github.com/rs/xid"
	"github.com/slclub/go-tips"
	"strings"
)

// 订单服务表
type OrderSrv struct {
	product ProductSrv
}

func (self *OrderSrv) GetList() []*app.Order {
	return nil
}

func (self *OrderSrv) GetOrder(serialno string) *app.Order {
	res := sqlhelper.ResWhere{}
	mc := datasrv.OrderDB.OrderFirst(context.Background(), &sqlhelper.ReqWhere{
		Where: &sqlhelper.Where{
			Conds: []*sqlhelper.CondItem{sqlhelper.WhereCond("OrderNo", serialno)},
		},
	}, &res)
	if res.MsgCode != code.SUCCESS {
		logq.InfoF("Recharge.First code:%v", mc)
		return nil
	}
	data_arr, ok := res.Data.(map[string]any)
	if !ok {
		return nil
	}
	rtn := &app.Order{}
	rtn.ConvFromMap(data_arr)
	return rtn
}

func (self *OrderSrv) Display(order *app.Order) map[string]any {
	if order == nil {
		return nil
	}
	rtn := order.ConvToMap()
	delete(rtn, "UUID")
	delete(rtn, "DeletedAt")
	proc := self.product.First(order.ProcID)
	proc_m := proc.ConvToMap()
	rtn["Proc"] = proc_m

	userSrv := UserSrv{}
	user := userSrv.GetUserWithUUID(order.UUID)
	user_m := user.ConvToMap()

	rtn["User"] = user_m
	return rtn
}

func (self *OrderSrv) FirmOrder(data map[string]any) *app.Order {
	proc_id, ok := data["ProcID"]
	num := tips.Int(data["Num"])
	procSrv := ProductSrv{}
	proc := procSrv.First(proc_id)
	if proc == nil {
		return nil
	}
	if num == 0 {
		num = 1
	}
	if !ok {
		return nil
	}
	ID := xid.New()
	uuid := tips.String(data["UUID"])
	order := self.FirstDB(&sqlhelper.Where{
		Conds: []*sqlhelper.CondItem{
			sqlhelper.WhereCond("UUID", uuid),
			sqlhelper.WhereCond("ProcID", proc_id),
			sqlhelper.WhereCond("State", helper.ORDER_STATE_SURE),
		},
		Formula: "{1} AND {2} AND {3}",
	})
	if order == nil {
		order = &app.Order{
			OrderNo: ID.String(),
			ProcID:  tips.Int(proc_id),
			Num:     num,
			Amount:  proc.PriceDiscount * num,
			State:   helper.ORDER_STATE_SURE,
			Finish:  helper.ORDER_FINISH_ING,
		}
	}

	order.UUID = uuid
	return order
}

func (self *OrderSrv) Save(order *app.Order) code.CODENO {
	res := sqlhelper.ResWhere{}
	mc := datasrv.OrderDB.OrderSave(context.Background(), &sqlhelper.ReqWhere{
		Object: order.ConvToMap(),
		Where: &sqlhelper.Where{
			Conds: []*sqlhelper.CondItem{sqlhelper.WhereCond("OrderNo", order.OrderNo)},
		},
	}, &res)
	if res.MsgCode != code.SUCCESS {
		logq.InfoF("Order.SAVE code:%v", mc)
		return res.MsgCode
	}
	return code.SUCCESS
}

func (self *OrderSrv) CallBackEncode(cb []string) string {
	if cb == nil {
		return ""
	}
	return strings.Join(cb, ",")
}

func (self *OrderSrv) CallBackDecode(cb string) []string {
	call_arr := []string{}
	if cb != "" {
		call_arr = strings.Split(cb, ",")
	}
	return call_arr
}

func (self *OrderSrv) FirstDB(where *sqlhelper.Where) *app.Order {
	res := sqlhelper.ResWhere{}
	mc := datasrv.OrderDB.OrderFirst(context.Background(), &sqlhelper.ReqWhere{
		Where: where,
	}, &res)
	if res.MsgCode != code.SUCCESS {
		logq.InfoF("OrderSrv.First code:%v", mc)
		return nil
	}
	data_arr, ok := res.Data.(map[string]any)
	if !ok || tips.String(data_arr["OrderNo"]) == "" {
		return nil
	}
	rtn := &app.Order{}
	rtn.ConvFromMap(data_arr)
	return rtn
}

// 充值记录表
type Recharge struct {
}

func (self *Recharge) GetListDB(where *sqlhelper.Where) []*app.Recharge {
	datapp := dbapp.OrderApi{}
	res := sqlhelper.ResWhere{}
	mc := datapp.RechargeSelect(context.Background(), &sqlhelper.ReqWhere{
		Where: where,
	}, &res)
	if res.MsgCode != code.SUCCESS {
		logq.InfoF("Recharge.GET code:%v", mc)
		return nil
	}
	data_arr, ok := res.Data.([]any)
	if !ok {
		return nil
	}

	rtn := []*app.Recharge{}
	for _, v := range data_arr {
		m, ok := v.(map[string]any)
		if !ok {
			continue
		}
		tmp := &app.Recharge{}
		tmp.ConvFromMap(m)
		rtn = append(rtn, tmp)
	}
	return rtn
}

func (self *Recharge) Save(p *app.Recharge, where *sqlhelper.Where) code.CODENO {
	res := sqlhelper.ResWhere{}
	if where == nil {
		where = &sqlhelper.Where{
			Conds: []*sqlhelper.CondItem{sqlhelper.WhereCond("ID", p.ID)},
		}
	}
	mc := datasrv.OrderDB.RechargeSave(context.Background(), &sqlhelper.ReqWhere{
		Object: p.ConvToMap(),
		Where:  where,
	}, &res)
	if res.MsgCode != code.SUCCESS {
		logq.InfoF("Recharge.SAVE code:%v", mc)
		return res.MsgCode
	}
	return code.SUCCESS
}

func (self *Recharge) FirstDB(where *sqlhelper.Where) *app.Recharge {
	res := sqlhelper.ResWhere{}
	mc := datasrv.OrderDB.RechargeFirst(context.Background(), &sqlhelper.ReqWhere{
		Where: where,
	}, &res)
	if res.MsgCode != code.SUCCESS {
		logq.InfoF("Recharge.First code:%v", mc)
		return nil
	}
	data_arr, ok := res.Data.(map[string]any)
	if !ok {
		return nil
	}
	rtn := &app.Recharge{}
	rtn.ConvFromMap(data_arr)
	return rtn
}
